package com.itheima.reggie.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
@Slf4j
public class CategoryServiceImpl implements CategoryService {


    @Autowired
    private CategoryMapper categoryMapper;

    //查询所有
    @Override
    @Cacheable(value = "category",key = "'findAll'")
    public List<Category> findAll() {
        log.info("----------redis---------");
        return categoryMapper.findAll();
    }


    //新增分类(菜品,套装公共后端)
    @Override
    @CacheEvict(value = "category",allEntries = true)
    public void save(Category category) {
        log.info("----------redis---------");
        //信息补全  雪花
        long id = IdUtil.getSnowflake(1, 1).nextId();
        category.setId(id);
      /*  //创建时间,更新时间
        category.setCreateTime(new Date()); //todo 后期改
        category.setUpdateTime(new Date());
        //创建人,更新人
        category.setCreateUser(1L);
        category.setUpdateUser(1L);*/

        //调用mapper新增
        categoryMapper.save(category);

    }


    //修改菜品及套餐
    @CacheEvict(value = "category",allEntries = true)
    @Override
    public void updateById(Category category) {
       /* //信息补全
        category.setUpdateUser(1L); //todo 后期修改
        category.setUpdateTime(new Date());*/
        //调用mapper修改
        categoryMapper.updateById(category);
    }


    //删除菜单 (菜单有菜品不能删除)
    @CacheEvict(value = "category",allEntries = true)
    @Override
    public void deleteById(long id) {
        //根据分类id查询菜品数量，判断
        Long countDish = categoryMapper.countDishByCategoryId(id);
        if (countDish > 0) {
            //return ResultInfo.error("该分类下有菜品不允许删除");
            throw new CustomException("该分类下有菜品，删除失败");
        }
        //根据分类id查询套餐数量，判断
        Long countSetmeal = categoryMapper.countSetmealByCategoryId(id);
        if (countSetmeal > 0) {
           // return ResultInfo.error("该分类下有套餐不允许删除");
            throw new CustomException("该分类下有套餐，删除失败");
        }
        //根据id删除分类
        categoryMapper.deleteById(id);
       // return ResultInfo.success();
    }

    //菜品管理 => 分类回显
    @Override
    @Cacheable(value = "category",key = "#type")
    public List<Category> findByType(Integer type) {
        // 构建条件
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(Category::getType, type); // 类型条件
        qw.orderByAsc(Category::getSort); // 排序

        // 调用mapper查询并返回
        return categoryMapper.selectList(qw);

    }
}
